﻿using UnityEngine;


public enum WeaponType
{
    Gun,
    Rifle,
    Rocket,
}

public abstract class IWeapon
{
    //protected int mAtkPlusValue;
    protected GameObject mGameObject;
    protected ParticleSystem mPariticle;
    protected LineRenderer mLine;
    protected Light mLight;
    protected AudioSource mAudio;

    protected ICharacter mOwner;
    protected float mEffectDisplayTime = 0f;

    protected WeaponBaseAttr mBaseAttr;

    public IWeapon(WeaponBaseAttr baseAttr, GameObject gameObject)
    {
        mBaseAttr = baseAttr;
        mGameObject = gameObject;

        Transform effect = mGameObject.transform.Find("Effect");
        mPariticle = effect.GetComponent<ParticleSystem>();
        mLine = effect.GetComponent<LineRenderer>();
        mLight = effect.GetComponent<Light>();
        mAudio = effect.GetComponent<AudioSource>();
    }

    public float AtkRange => mBaseAttr.AtkRange;

    public int Atk => mBaseAttr.Atk;

    public ICharacter Owner
    {
        set => mOwner = value;
    }

    public GameObject GameObject => mGameObject;

    public void Update()
    {
        if (mEffectDisplayTime > 0)
        {
            mEffectDisplayTime -= Time.deltaTime;
            if (mEffectDisplayTime <= 0)
            {
                DisableEffectDisplay();
            }
        }
    }

    public void Fire(Vector3 targetPos)
    {
        //显示枪口特效
        PlayMuzzleEffect();

        //显示子弹轨迹
        PlayBulletEffect(targetPos);

        //设置特效显示时间
        SetEffectDisplayTime();

        //播放声音
        PlaySound();
    }

    protected abstract void SetEffectDisplayTime();

    protected virtual void PlayMuzzleEffect()
    {
        mPariticle.Stop();
        mPariticle.Play();
        mLight.enabled = true;
    }

    protected abstract void PlayBulletEffect(Vector3 targetPos);

    protected virtual void DoPlayBulletEffect(float width, Vector3 targetPos)
    {
        mLine.enabled = true;
        mLine.startWidth = width;
        mLine.endWidth = width;
        mLine.SetPosition(0, mGameObject.transform.position);
        mLine.SetPosition(1, targetPos);
    }

    protected abstract void PlaySound();

    protected virtual void DoPlaySound(string _clipName)
    {
        AudioClip clip = FactoryManager.AssetFactory.LoadAudioClip(_clipName); //TODO
        mAudio.clip = clip;
        mAudio.Play();
    }

    /// <summary>
    /// 关闭特效
    /// </summary>
    private void DisableEffectDisplay()
    {
        mLight.enabled = false;
        mLine.enabled = false;
    }
}